package com.wjj.controller;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wjj.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class GetJsonInforController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/")
    public void getJsonInfor() {
        getJsonInfo();
    }

    /**
     *每个月6号执行
     */
    @Scheduled(cron = "0 0 0 6 * ?")
    public void scheduledTask() {
        getJsonInfo();
    }

    private void getJsonInfo() {
        String url = "http://localhost:8080/json";
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
        String jsonBody = exchange.getBody();

        JsonObject jsonObject = JsonParser.parseString(jsonBody).getAsJsonObject();

        Person person = new Person();

        person.setName(jsonObject.get("dnhxhhqidbxqhabh").getAsString());
        System.out.println("Name: " + person.getName());
    }
}